﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataTool;
using System.Activities.Persistence;
using System.Xml.Linq;
//using workflowMVC.Models;
//using workflowMVC.Common;

namespace Common
{
    public class ValidateTask 
    {
        public Guid Id { get; set; }
        public byte Status { get; set; }
        public string NumCode { get; set; }
        public byte TaskKind { get; set; }

        public void UpdateStatus(byte status)
        {
            wfdbEntities db = new wfdbEntities();
            db.TASKs.Where(x => x.Id == Id).FirstOrDefault().Status = status;
            db.SaveChanges();
        }

        public void CreateLog(byte Logkind, string Username, string Note)
        {
            wfdbEntities db = new wfdbEntities();
            LOG l = new LOG()
            {
                Date = DateTime.Now,
                Id= Guid.NewGuid(),
                Kind = Logkind,
                Note = Note,
                Task = Id,
                Writer = Username,
            };
            db.LOGs.AddObject(l);
            db.SaveChanges();
        }

        public virtual void SaveChange()
        {
            wfdbEntities db = new wfdbEntities();
            var t = db.TASKs.Where(x => x.Id == Id).FirstOrDefault();
            t.Status = Status;
            t.NumCode = NumCode;
            t.Kind = TaskKind;
            db.SaveChanges();
        }

        public virtual void Create()
        {
            TASK t = new TASK()
            {
                NumCode = this.NumCode,
                Id = this.Id,
                Status = 1,
                Kind = this.TaskKind
            };
            wfdbEntities db = new wfdbEntities();
            db.TASKs.AddObject(t);
            db.SaveChanges();
        }
    }

    
    public class SimpleTask : ValidateTask
    {
        public string Reason { get; set; }
        public override void Create()
        {
            base.Create();
            wfdbEntities db = new wfdbEntities();
            db.TASKs.Where(x => x.Id == Id).FirstOrDefault().Reason = Reason;
            db.SaveChanges();
        }
        public override void SaveChange()
        {
            base.SaveChange();
            wfdbEntities db = new wfdbEntities();
            var t = db.TASKs.Where(x => x.Id == Id).FirstOrDefault();
            t.Reason = Reason;
        }
    }

    public class ExcemptTuitionTask : ValidateTask
    {        
        public string Hometown { get; set; }
        public string ParentName { get; set; }
        public string Street { get; set; }
        public string Ward { get; set; }
        public string District { get; set; }
        public string Province { get; set; }
        public string Type { get; set; }
        public string Tuition { get; set; }
        public string Fault { get; set; }

        public override void Create()
        {
            base.Create();
            wfdbEntities db = new wfdbEntities();
            var t = db.TASKs.Where(x => x.Id == Id).FirstOrDefault();
            t.HomeTown = Hometown;
            t.ParentName = ParentName;
            t.Street = Street;
            t.Ward = Ward;
            t.District = District;
            t.Province = Province;
            t.Type = Type;
            t.Tuition = Tuition;
            t.Fault = Fault;
            db.SaveChanges();
        }
        public override void SaveChange()
        {
            base.SaveChange();
            wfdbEntities db = new wfdbEntities();
            var t = db.TASKs.Where(x => x.Id == Id).FirstOrDefault();
            t.HomeTown = Hometown;
            t.ParentName = ParentName;
            t.Street = Street;
            t.Ward = Ward;
            t.District = District;
            t.Province = Province;
            t.Tuition = Tuition;
            t.Fault = Fault;
            db.SaveChanges();
        }
    }
}
